package Cain.moveMenu 
{
	import Cain.MkMc.Bk;
	import Cain.pcxbook.LoaderAutoSize;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	/**
	 * 選項
	 * @author cain in liontravel 2010
	 */
	public class Item extends Sprite 
	{
		//private var pcx:PcxAutoSize;
		private var pcx:LoaderAutoSize
		private var ww:Number;
		private var hh:Number;
		private var pcx_path:String;
		private var link:String;
		private var myId:int;
		private var bk:Bk;
		private var back_color:uint;
		private var windows:String;
		/**
		 * 建構
		 * @param	_ww 寬
		 * @param	_hh 高
		 * @param	_id 序號
		 * @param	_color 顏色
		 */
		public function Item(_ww:Number,_hh:Number,_id:int,_color:uint = 0) 
		{
			ww = _ww;
			hh = _hh;
			myId = _id;
			back_color = _color;
			setup();
		}
		public function get id():int 
		{
			return myId;
		}
		/**
		 * 圖像路徑
		 */
		public function set pcxPath(_url:String):void 
		{
			pcx_path = _url;
		}
		/**
		 * 圖像載入
		 * @param	_url 網址
		 * @param	_load_class 載入動畫
		 */
		public function load(_url:String = "",_load_class:String = ""):void 
		{
			if (_url != "") 
			{
				pcx_path = _url;
			}
			pcx.addEventListener(Event.COMPLETE, onCOMPLETE);
			pcx.load(pcx_path);
			addChild(pcx);
			function onCOMPLETE(e:Event):void 
			{
				pcx.removeEventListener(Event.COMPLETE, onCOMPLETE);
				pcx.width = ww;
				pcx.height = hh;
				dispatchEvent(e);
				buttonMode = true;
				pcx.mask = null;
			}
		}
		/**
		 * 設定連結
		 * @param	_link 連結
		 * @param	_windows 視窗
		 */
		public function itemlink(_link:String,_windows:String = "_self"):void 
		{
			link = _link;
			windows = _windows;
			addEventListener(MouseEvent.CLICK, onCLICK);
		}
		
		private function onCLICK(e:MouseEvent):void 
		{
			navigateToURL(new URLRequest(link), windows);
		}
		private function setup():void
		{
			addEventListener(Event.ADDED_TO_STAGE, onADDED_TO_STAGE);
			bk = new Bk(ww, hh, back_color);
			pcx = new LoaderAutoSize();
			pcx.setup(ww, hh);
		}
		private function onADDED_TO_STAGE(e:Event):void 
		{
			addEventListener(Event.REMOVED_FROM_STAGE, onREMOVED_FROM_STAGE);
			init();
		}
		private function init():void
		{
			addChild(bk);
			addEventListeners();
		}
		
		private function addEventListeners():void
		{
			
		}
		private function removeEventListeners():void
		{
			bk = null;
			removeEventListener(MouseEvent.CLICK, onCLICK);
		}
		private function onREMOVED_FROM_STAGE(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onADDED_TO_STAGE);
			removeEventListener(Event.REMOVED_FROM_STAGE, onREMOVED_FROM_STAGE);
			removeEventListeners();
		}
	}
	
}